package com.code;


import java.io.File;
import java.io.FileWriter;

public class BaseCodeItem {

    private String packageName;
    private String content;
    private String fileName;
    private String lastPath;

    private BaseCodeItem(Builder builder) {
        packageName = builder.packageName;
        content = builder.content;
        fileName = builder.fileName;
        lastPath = builder.lastPath;
    }

    public String getLastPath() {
        return lastPath;
    }

    public static Builder builder() {
        return new Builder();
    }


    public void write(String path) {
        File cache = new File(path);
        if (!cache.exists()) {
            cache.mkdirs();
        }
        StringBuilder builder = new StringBuilder();
        builder.append(packageName);
        builder.append("\n");
        builder.append(content);
        try {
            File file = new File(path, fileName);
            FileWriter fileWriter = new FileWriter(file);
            fileWriter.write(builder.toString());
            fileWriter.flush();
            fileWriter.close();
        } catch (Exception e) {
        }
    }


    public static final class Builder {
        private String packageName;
        private String content;
        private String fileName;
        private String lastPath;

        private Builder() {
        }


        public Builder packageName(String packageName) {
            this.packageName = packageName;
            return this;
        }

        public Builder fileName(String fileName) {
            this.fileName = fileName;
            return this;
        }

        public Builder content(String content) {
            this.content = content;
            return this;
        }

        public Builder lastPath(String lastPath) {
            this.lastPath = lastPath;
            return this;
        }

        public BaseCodeItem build() {
            return new BaseCodeItem(this);
        }
    }
}
